package com.moli.iov.admin.web.controller;

import com.alibaba.fastjson.JSONObject;
import com.moli.iov.admin.component.CacheManger;
import com.moli.iov.admin.model.vo.req.BaseVo;
import com.moli.iov.exception.BusinessException;
import com.moli.iov.sys.constants.Constant;
import com.moli.iov.sys.enums.UserTypeEnum;
import com.moli.iov.sys.model.po.User;
import com.moli.iov.util.CommonUtils;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;

import static org.springframework.http.HttpHeaders.*;

/**
 * <p>
 *
 * </p>
 *
 * @author zql
 * @since 2019/9/3 15:01
 */
@Slf4j
public class BaseController {

  @Autowired
  HttpServletRequest request;

  @Autowired
  private CacheManger cacheManger;

  /**
   * 获取用户信息
   * @return
   */
  public BaseVo getBaseVo(){

    /*BaseVo baseVo = new BaseVo();
    baseVo.setSysUserId(1L);
    baseVo.setSysUserType(1);
    baseVo.setSysRealName("管理员");
    baseVo.setSysAccount("admin");
    baseVo.setSysCompanyId(null);
    baseVo.setSysCompanyName(null);*/
    String account = (String)request.getAttribute(Constant.DEFAULT_REQUEST_PERMISSION);

    User user = cacheManger.getUser(account);
    if(user == null){
      throw new BusinessException("请重新登录");
    }

    BaseVo baseVo = assembleBaseVo(user);
    if(UserTypeEnum.COMPANY.getFlag() == baseVo.getSysUserType() &&
        baseVo.getSysCompanyId() == null){
      throw new BusinessException("操作员企业信息异常，请重新登录");
    }
    baseVo.setPageNum(null);
    baseVo.setPageSize(null);
    log.info("操作员user信息：{}", user);
    return baseVo;
  }

  /**
   * 组装操作员信息
   * @param baseVo
   * @return
   */
  public BaseVo assembleUserInfo(BaseVo baseVo){
    CommonUtils.copyProperties(getBaseVo(), baseVo);

    log.info("操作员baseVo信息：{}", JSONObject.toJSONString(baseVo));
    return baseVo;
  }

  private BaseVo assembleBaseVo(User user){
    BaseVo baseVo = new BaseVo();
    baseVo.setSysUserId(user.getUserId());
    baseVo.setSysAccount(user.getAccount());
    baseVo.setSysRealName(user.getRealName());
    baseVo.setSysUserType(user.getUserType());
    baseVo.setSysCompanyId(user.getCompanyId());
    baseVo.setSysCompanyName(user.getCompanyName());
    baseVo.setPageNum(null);
    baseVo.setPageSize(null);
    return baseVo;
  }

  /**
   * Get http headers collection.
   * @return Http headers collection.
   */
  public HttpHeaders getHttpHeaders(){
      HttpHeaders httpHeaders = new HttpHeaders();
      httpHeaders.add(ACCESS_CONTROL_ALLOW_ORIGIN,request.getHeader("Origin"));
      httpHeaders.add(ACCESS_CONTROL_ALLOW_CREDENTIALS, "true");
      httpHeaders.add(ACCESS_CONTROL_ALLOW_METHODS, "POST, GET, PATCH, DELETE, PUT");
      httpHeaders.add(ACCESS_CONTROL_MAX_AGE, "3600");
      httpHeaders.add(ACCESS_CONTROL_ALLOW_HEADERS, "Origin, X-Requested-With, Content-Type, Accept");
      return httpHeaders;
  }

  /**
   * Set http headers collection.
   * @param response Http servlet response.
   */
  public void setHeaders(HttpServletResponse response){
      response.setHeader(ACCESS_CONTROL_ALLOW_ORIGIN,request.getHeader("Origin"));
      response.setHeader(ACCESS_CONTROL_ALLOW_CREDENTIALS, "true");
      response.setHeader(ACCESS_CONTROL_ALLOW_METHODS, "POST, GET, PATCH, DELETE, PUT");
      response.setHeader(ACCESS_CONTROL_MAX_AGE, "3600");
      response.setHeader(ACCESS_CONTROL_ALLOW_HEADERS, "Origin, X-Requested-With, Content-Type, Accept");
  }


}
